/**
 * Configurator.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package extjsdyntran.translation;

import java.util.Hashtable;
import java.util.Properties;
import org.json.*;

import extjsdyntran.servlets.Constants;


/**
 * @description Main class for Translation Editor Grid to support record deletion/creation/update
 * 
 */
public class Configurator {

	protected Hashtable<String, Properties> languages = new Hashtable<String, Properties>();
	
	
	private String getError(Exception e){
		try{
			return getResponse(false,e.getMessage(),"");
			}catch(Exception e1){return Constants.json_false;}
	}
	
	//creates Ext json records for loading in grid
	public String getTranslationRecords(String language) {
		try {

			Properties lang = (Properties) languages.get(language);
			if (lang == null)
				return getResponse(false,Constants.err1,"");
						
			return JSONRecordProperties.getJsonRecords(lang);

		} catch (Exception e) {
			return getError(e);
		}
	}

	private String getResponse(boolean status, String msg, String data) throws JSONException{
	
		JSONObject res = new JSONObject();
		res.put("success", status);
		res.put("msg",msg);
		res.put(Constants.param_data, data);
		return res.toString();
	}
	
	public String deleteRecords(String language, String values) {
		try {

			Properties lang = (Properties) languages.get(language);
			if (lang == null)
				return getResponse(false,Constants.err1,"");
			
			JSONArray jsarr =null;
			try{
				jsarr = new JSONArray(values);
			} catch (Exception e){
				String key = (String)(new JSONTokener(values)).nextValue();
				
				if (lang.containsKey(key))
					lang.remove( key);
				
				return getResponse(true,"","[]");
			}
			
			int i = jsarr.length();
			int j = 0;
			while (j<i){
				String key = jsarr.getString(j);				
				if (lang.containsKey(key))
					lang.remove( key);
				j++;
			};			
			return getResponse(true,"","[]");

		} catch (Exception e) {
			return getError(e);
		}		
		
	}
	
	public String updateRecords(String language, String values) {
		try {

			Properties lang = (Properties) languages.get(language);
			if (lang == null)
				return getResponse(false,Constants.err1,"");
			
			JSONArray jsarr=null;
			
			try{
				jsarr = new JSONArray(values);
			} catch (Exception e){
				JSONObject jsobj = new JSONObject (values);
				lang.remove(jsobj.get("key"));
				lang.put(jsobj.get("key"), jsobj.get("value"));
				return getResponse(true,"", "[" +  values + "]");
			}
			
			int i = jsarr.length();
			int j = 0;
			while (j<i){	
				JSONObject jsobj = jsarr.getJSONObject(j);
				lang.remove(jsobj.get("key"));
				lang.put(jsobj.get("key"), jsobj.get("value"));
				j++;
			};		
			return getResponse(true,"",values);

		} catch (Exception e) {
			return getError(e);
		}		
			
	}
	
	
	private boolean setData(Properties lang,String key, String value){
		boolean res = lang.containsKey(key);
		if (res) return !res;		
		lang.put(key, value);		
		return res;		
	}
	
	public String createRecords(String language, String values) {
		try {

			Properties lang = (Properties) languages.get(language);
			if (lang == null)
				return getResponse(false,Constants.err1,"");
			
			JSONArray jsarr=null;
			
			try{
				jsarr = new JSONArray(values);
			} catch (Exception e){
				JSONObject jsobj = new JSONObject (values);
				if (setData(lang, (String)jsobj.get("key"),(String)jsobj.get("value"))){
					return getResponse(true,"","[" +  values + "]");	
				} else return getResponse(false,Constants.err2,"");
				
			}
			
			int i = jsarr.length();
			int j = 0;
			while (j<i){	
				JSONObject jsobj = jsarr.getJSONObject(j);
				setData(lang, (String)jsobj.get("key"),(String)jsobj.get("value"));
				j++;
			};			
			return getResponse(true,"",values);

		} catch (Exception e) {
			return getError(e);
		}		
			
	}	
	
}
